//
//  LRCellBottomView.swift
//  LRWeibo
//
//  Created by 雷路荣 on 15/12/21.
//  Copyright © 2015年 leilurong. All rights reserved.
//

import UIKit
/**
 *  声明代理协议和方法
 */
protocol LRCellBottomViewDelegate: NSObjectProtocol {
    ///转发按钮点击
    func forwardButtonDidClick()
    ///评论按钮点击
    func commentButtonDidClick()
    ///点赞按钮点击
    func lickButtonDidClick()
}

class LRCellBottomView: UIView {
    //代理协议
    weak var delegate: LRCellBottomViewDelegate?
    
    //模型数据
    var statuses: LRStatues? {
        didSet{
//            forwardButton.setTitle("\(statuses!.reposts_count)", forState: UIControlState.Normal)
//            commentButton.setTitle("\(statuses!.comments_count)", forState: UIControlState.Normal)
//            likeButton.setTitle("\(statuses!.attitudes_count)", forState: UIControlState.Normal)
            setButton(forwardButton, count: (statuses!.reposts_count))
            setButton(commentButton, count: (statuses!.comments_count))
            setButton(likeButton, count: (statuses!.attitudes_count))
        }
    }
    
    //设置按钮文字
    func setButton(button:UIButton, count: Int) {
        if count > 10000 {
            let final = CGFloat(count)/10000.0
            //设置一位小数
            var titleText = String(format: "%.1f万", arguments: [final])
            //当为整数时去掉.0
            titleText = titleText.stringByReplacingOccurrencesOfString(".0", withString: "")
            button.setTitle(titleText, forState: UIControlState.Normal)
        }else if count > 0 {
            button.setTitle("\(count)", forState: UIControlState.Normal)
        }else if count == 0 {
            //button.setTitle("\(0)", forState: UIControlState.Normal)
            switch button {
            case forwardButton:
                button.setTitle("转发", forState: UIControlState.Normal)
            case commentButton:
                button.setTitle("评论", forState: UIControlState.Normal)
            default:
                button.setTitle("赞", forState: UIControlState.Normal)
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }
    func prepareUI() {
        //设置背景色
        backgroundColor = UIColor(white: 0.98, alpha: 1)
        addSubview(forwardButton)
        addSubview(commentButton)
        addSubview(likeButton)
        addSubview(sepImageViewFirst)
        addSubview(sepImageViewSecond)
        // 添加约束
        /// 平铺子控件
        /// self: 父控件
        /// views: 哪些子控件要平铺
        /// insets边距
        self.ff_HorizontalTile([forwardButton,commentButton,likeButton], insets: UIEdgeInsetsZero)
        sepImageViewFirst.ff_AlignHorizontal(type: ff_AlignType.CenterRight, referView: forwardButton, size: nil)
        sepImageViewSecond.ff_AlignHorizontal(type: ff_AlignType.CenterRight, referView: commentButton, size: nil)
    }
    // MARK: - 按钮点击事件
    //转发按钮点击
    func forwardButtonClick() {
        delegate?.forwardButtonDidClick()
    }
    //评论按钮点击
    func commentButtonClick() {
        delegate?.commentButtonDidClick()
    }
    //点赞按钮点击
    func lickButtonClick() {
        delegate?.lickButtonDidClick()
    }
    
    //MARK: - 懒加载
    /// 转发按钮
    private lazy var forwardButton: UIButton = {
        let button = UIButton(imageName: "timeline_icon_retweet", title: "转发", sizeFont: 14, textColor: UIColor.lightGrayColor())
        button.addTarget(self, action: "forwardButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    /// 评论
    private lazy var commentButton: UIButton = {
        let button = UIButton(imageName: "timeline_icon_comment", title: "评论", sizeFont: 14, textColor: UIColor.lightGrayColor())
        button.addTarget(self, action: "commentButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    /// 赞按钮
    private lazy var likeButton: UIButton = {
        let button = UIButton(imageName: "timeline_icon_unlike", title: "赞", sizeFont: 14, textColor: UIColor.lightGrayColor())
        button.addTarget(self, action: "lickButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    /// 分割线1
    private lazy var sepImageViewFirst: UIImageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line_highlighted"))
    /// 分割线2
    private lazy var sepImageViewSecond: UIImageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line_highlighted"))
    
}



















